#coding=utf-8
  
import urllib.request
import urllib
import time
import socket
 
ip_check_url = 'http://www.baidu.com/'
socket_timeout = 10
  
# Check proxy
def check_proxy(protocol, pip): 
  try:
    proxy_handler = urllib.request.ProxyHandler({protocol:pip})
    opener = urllib.request.build_opener(proxy_handler)
    urllib.request.install_opener(opener)
 
    req = urllib.request.Request(ip_check_url)
    time_start = time.time()
    conn = urllib.request.urlopen(req)
    # conn = urllib.request.urlopen(ip_check_url)
    time_end = time.time()
    detected_pip = conn.read()
     
    proxy_detected = True
  
  except urllib.request.HTTPError as e:
    print("ERROR: Code ", e)
    return False
  except Exception as detail:
    print("ERROR: ", detail)
    return False
  
  return proxy_detected
 
def check_http_proxy(proxy):
  socket.setdefaulttimeout(socket_timeout)
  protocol = "http"
  return check_proxy(protocol, proxy)

def main():
  socket.setdefaulttimeout(socket_timeout)
  
  print
   
  protocol = "http"
  current_proxy = "212.82.126.32:80"
  proxy_detected = check_proxy(protocol, current_proxy)
  if proxy_detected:
    print (" WORKING: " , current_proxy)
  else:
    print (" FAILED: " ,  current_proxy )
 
if __name__ == '__main__':
  main()